// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "title": {
    "rules": [
      {
        "format": "string"
      },
      {
        "minLength": 2,
        "maxLength": 8
      }
    ],
    "title": "应用标题",
    "label": "应用标题"
  },
  "ftitle": {
    "rules": [
      {
        "format": "string"
      },
      {
        "minLength": 2,
        "maxLength": 20
      }
    ],
    "title": "应用副标题",
    "label": "应用副标题"
  },
  "keyword": {
    "rules": [
      {
        "format": "string"
      },
      {
        "minLength": 2,
        "maxLength": 20
      }
    ],
    "title": "应用关键词",
    "label": "应用关键词"
  },
  "image": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "应用图片",
    "label": "应用图片"
  },
  "describe": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "应用说明",
    "label": "应用说明"
  },
  "textarea_font_number": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "title": "多行文本框输入的最大字数",
    "defaultValue": 500,
    "label": "多行文本框输入的最大字数"
  },
  "textarea_placeholder": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "富文本输入框的说明文本",
    "label": "富文本输入框的说明文本"
  },
  "case": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "案例内容",
    "label": "案例内容"
  },
  "app_type": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "title": "应用类型",
    "label": "应用类型"
  },
  "type": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "应用分类",
    "label": "应用分类"
  },
  "use_number": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "title": "使用人数",
    "defaultValue": 0,
    "label": "使用人数"
  },
  "sort": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "title": "排序",
    "defaultValue": 0,
    "label": "排序"
  },
  "is_show_textarea": {
    "rules": [
      {
        "format": "bool"
      }
    ],
    "title": "是否显示多行文本框",
    "defaultValue": true,
    "label": "是否显示多行文本框"
  },
  "is_show_copy_link": {
    "rules": [
      {
        "format": "bool"
      }
    ],
    "title": "是否显示粘贴内容连接按钮",
    "defaultValue": false,
    "label": "是否显示粘贴内容连接按钮"
  },
  "create_uid": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "创建人",
    "defaultValue": {
      "$env": "uid"
    },
    "label": "创建人"
  },
  "create_date": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "title": "创建时间",
    "defaultValue": {
      "$env": "now"
    },
    "label": "创建时间"
  }
}

const enumConverter = {}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
